home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / tileex / tileex.bas < prev   
BASIC Source File  |  1995-03-10  |  14KB  |  307 lines

  1. 'This is a small program using tile graphics. This source code is the property
  2. 'of James Tonn. You may modify this source code and use it in your own
  3. 'programs. You may use it in a written publication provided that it is not
  4. 'altered, and that you give credit to the author (James Tonn).
  5. 'I hope this code will help you in your programming.
  6. '   -Jimmy Tonn 8/94
  7. '   sltonn@phoenix.princeton.edu
  8. '   sltonn@eden.rutgers.edu
  9. '   jtonn@nerc1.nerc.com
  10. '   75022.401@compuserve.com
  11.  
  12. DEFINT A-Z  'declare all variables starting with a through z as integers
  13. SCREEN 13   'set screen mode 13, 320x200x256
  14. CLS         'clear screen
  15.  
  16. RESTORE world   'restore data pointer so all data reading starts after label
  17.                 ' "world"
  18.  
  19. CONST ACROSS = 11 'this is the number of across tiles that will appear on
  20.                   'the screen at a time, the total number of across tiles
  21.                   'is stored in the variable tacross
  22. CONST DOWN = 11   'number of down tiles on screen... total down tiles are
  23.                   'stored in tdown
  24.  
  25. DIM blankimg(116) 'create an array for the blank tile
  26.                   'this tile will be totally black, all zero's
  27. DIM grassimg(116) 'create an array for the grass tile's image, 116 bytes
  28. DIM treeimg(116)  'create an array for the tree tile's image, 116 bytes
  29. DIM playerimg(116)'create as array for the player tile's image, 116 bytes
  30.  
  31. READ tacross, tdown  'get these values from the big data block "world"
  32.                      'at the end of this program
  33.  
  34. TYPE tiletype          'create a type called tiletype
  35.     x AS INTEGER       'the tile's x pixel coord
  36.     y AS INTEGER       'the tile's y pixel coord
  37.     style AS INTEGER   'the tile's style
  38. END TYPE
  39.  
  40. DIM tile(tacross, tdown) AS tiletype 'declare array "tile" as tiletype
  41.  
  42. TYPE playertype  'create a type called playertype
  43.     x AS INTEGER 'x tile where the player is
  44.     y AS INTEGER 'y tile where the player is
  45.     px AS INTEGER 'the players "permanant" x tile, the tile that he appears
  46.                   'in out of the tiles on the screen
  47.     py AS INTEGER 'the players "permanant" y tile
  48. END TYPE
  49.  
  50. DIM player AS playertype 'declare the variable player as a playertype
  51.  
  52. READ player.x, player.y  'read player x and y coords from data block "world"
  53. player.px = 6
  54. player.py = 6
  55.  
  56. DEF FNSetlocs    'function to set pixel locs of tiles that appear on screen
  57.    FOR a = 1 TO ACROSS
  58.       FOR d = 1 TO DOWN
  59.          tile(a, d).x = 15 * a  'set pixel x location of tile
  60.          tile(a, d).y = 15 * d  'set pixel y location of tile
  61.       NEXT d                    '(all tiles are 15x15 pixels)
  62.    NEXT a
  63. END DEF
  64.  
  65. DEF FNSetAtts   'function to read the styles of all tiles from data block
  66.    FOR d = 1 TO tdown
  67.       FOR a = 1 TO tacross
  68.          READ tile(a, d).style 'read the tile's style
  69.       NEXT a
  70.    NEXT d
  71. END DEF
  72.  
  73.  
  74. 'this next routine draws the tile on the screen
  75. 'the tx and ty variables are the tile numbers out of all the tiles
  76. 'the cx and cy variables are locations of the tile spaces where the tiles
  77. 'will be displayed on the screen
  78. DEF FNDrawTile (tx, ty, cx, cy)
  79.     SELECT CASE tile(tx, ty).style  'check the tile's style
  80.           CASE 0  'if it's 0, draw an array of zero's (a black tile)
  81.               PUT (tile(cx, cy).x, tile(cx, cy).y), blankimg, PSET
  82.           CASE 1  'if it's 1, draw the grass tile
  83.               PUT (tile(cx, cy).x, tile(cx, cy).y), grassimg, PSET
  84.           CASE 2  'if it's 2, draw the tree tile
  85.               PUT (tile(cx, cy).x, tile(cx, cy).y), treeimg, PSET
  86.     END SELECT
  87. END DEF
  88.  
  89. 'this draws all the visible tiles on the screen
  90. 'it will draw from 5 tiles to the left of the player to 5 tiles to his right
  91. 'and from 5 tiles up to 5 tiles down
  92. DEF FNDrawScreen
  93.    FOR rela = -5 TO 5
  94.       FOR reld = -5 TO 5
  95.            dummy = FNDrawTile(player.x + rela, player.y + reld, player.px + rela, player.py + reld)
  96.       NEXT reld
  97.    NEXT rela
  98. END DEF
  99.  
  100. 'this function draws the player on the screen in his "permanant" location
  101. DEF FNDrawPlayer
  102.    PUT (tile(player.px, player.py).x, tile(player.px, player.py).y), playerimg, PSET
  103. END DEF
  104.  
  105. DEF FNDisplayImg 'this function will read an images colors from a data block
  106.                  'and display the image on the screen. Before calling this
  107.                  'function, reset the data pointer to the beginning of the
  108.                  'data block for the image.
  109.    FOR dp = 1 TO 15
  110.       FOR ap = 1 TO 15
  111.          READ att                     'read the value at the point in the data block
  112.          PSET (ap + 10, dp + 10), att 'put the pixel on the screen with the
  113.                                       'color that was read from the data
  114.                                       'block
  115.       NEXT ap
  116.    NEXT dp
  117. END DEF
  118.  
  119. '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  120. ' * * * * * * * * * * * * End of Function Defs* * * * * * * * * * * * * *
  121.  
  122. dummy = FNSetlocs   'set the locations for the on-screen tiles
  123. dummy = FNSetAtts   'set the styles for all the tiles
  124.  
  125. GET (11, 11)-(25, 25), blankimg 'get an array that is all zero's, which will
  126.                                 'the black tile that we put around the edges
  127.                                 'of the map. No data reading is needed since
  128.                                 'the screen is already totally black. All
  129.                                 'we need to do is grab a block.
  130.  
  131. RESTORE grassdata   'restore the data pointer to the beginning of the data
  132.                     'block that stores the grass tile's image. If you're
  133.                     'reseting the data pointer to load the images, do it
  134.                     'after calling FNSetLocs and FNSetAtts, because they
  135.                     'need the data pointer in a certain place
  136. dummy = FNDisplayImg  'call routine to read and display the image
  137. GET (11, 11)-(25, 25), grassimg 'get the image into array "grassimg"
  138. PUT (11, 11), grassimg, XOR     'put image over existing one to clear it
  139.  
  140. RESTORE treedata 'restore data pointer to beginning of the tree tile's image
  141. dummy = FNDisplayImg 'read and display the image
  142. GET (11, 11)-(25, 25), treeimg  'get the image into array "treeimg"
  143. PUT (11, 11), treeimg, XOR  'put image on top of the old one to clear it
  144.  
  145. RESTORE playerdata 'restore pointer to beginning of player tile's image
  146. dummy = FNDisplayImg 'read and display the image
  147. GET (11, 11)-(25, 25), playerimg  'get the image into array "playerimg"
  148. PUT (11, 11), playerimg, XOR  'put image on top of the old one to clear it
  149.  
  150. dummy = FNDrawScreen  'draw the screen once
  151. dummy = FNDrawPlayer  'draw the player once
  152.  
  153.  
  154. DO
  155.   kbd$ = INKEY$            'get a "transparent" input
  156.   IF kbd$ <> "" THEN       'if there actually was a user input, then...
  157.     kbd$ = RIGHT$(kbd$, 1) 'get the first byte of the input (this is needed
  158.                            'if you want to look for input from the cursor
  159.                            'keys)
  160.     SELECT CASE kbd$
  161.           CASE CHR$(27)    'user pressed escape key
  162.               END          'end the program
  163.           CASE CHR$(72) 'user pressed up arrow
  164.               IF tile(player.x, player.y - 1).style <> 2 THEN
  165.               '^^^ check if the tile the player is about to move into is
  166.               'already occupied by a tree.
  167.               'if you check for a tree, you really don't need to check if
  168.               'the player is going to go past the line of trees at the
  169.               'edge with the other method, because if you check for a tree,
  170.               'it will stop you before you go past the line anyway I just
  171.               'added them both in case the reader wanted to use a certain
  172.               'method for some reason.
  173.                
  174.                 IF player.y - 1 > 5 THEN     'make sure he doesn't go past
  175.                                              'the line of trees. You will
  176.                                              'probably have to change this
  177.                                              'number around if you change the
  178.                                              'player's view screen size
  179.                   player.y = player.y - 1    'decrease player y tile by one
  180.                 END IF
  181.                 dummy = FNDrawScreen       'draw the screen
  182.                 dummy = FNDrawPlayer       'put the player on the screen
  183.               END IF
  184.           CASE CHR$(80) 'user pressed down arrow
  185.               IF tile(player.x, player.y + 1).style <> 2 THEN
  186.               '^^^ check if the tile the player is about to move into is
  187.               'already occupied by a tree.
  188.                 IF player.y + 1 < (tdown - 5) THEN 'keep player on screen
  189.                   player.y = player.y + 1    'increase player y tile by one
  190.                 END IF
  191.                 dummy = FNDrawScreen       'draw the screen
  192.                 dummy = FNDrawPlayer       'put the player on the screen
  193.               END IF
  194.           CASE CHR$(75) 'user pressed left arrow
  195.               IF tile(player.x - 1, player.y).style <> 2 THEN
  196.               '^^^ check if the tile the player is about to move into is
  197.               'already occupied by a tree.
  198.  
  199.                 IF player.x - 1 > 5 THEN   'keep player on screen
  200.                   player.x = player.x - 1    'decrease player x tile by one
  201.                 END IF
  202.                 dummy = FNDrawScreen       'draw the screen
  203.                 dummy = FNDrawPlayer       'put the player on the screen
  204.               END IF
  205.           CASE CHR$(77) 'user pressed right arrow
  206.               IF tile(player.x + 1, player.y).style <> 2 THEN
  207.               '^^^ check if the tile the player is about to move into is
  208.               'already occupied by a tree.
  209.  
  210.                 IF player.x < (tacross - 6) THEN  'keep player on screen
  211.                   player.x = player.x + 1    'increase player x tile by one
  212.                 END IF
  213.                 dummy = FNDrawScreen       'draw the screen
  214.                 dummy = FNDrawPlayer       'put the player on the screen
  215.               END IF
  216.     END SELECT
  217.   END IF
  218. LOOP        'restart the main loop
  219.  
  220.  
  221. world:
  222.  
  223. DATA 30,30
  224. DATA 15,15
  225. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  226. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  227. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  228. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  229. DATA 0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0
  230. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  231. DATA 0,0,0,0,2,2,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  232. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,0,0,0,0,0
  233. DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1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ATA 0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0
  250. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  251. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  252. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  253. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  254. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  255.  
  256. grassdata:
  257. DATA 0,0,0,0,2,0,0,0,148,0,0,2,0,0,0
  258. DATA 0,2,0,0,0,0,0,0,0,0,0,0,0,0,141
  259. DATA 0,0,0,0,142,0,0,2,0,0,0,0,0,0,0
  260. DATA 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0
  261. DATA 0,2,0,2,0,145,0,0,0,142,0,0,0,0,0
  262. DATA 0,0,0,0,0,0,0,0,2,0,0,0,0,0,2
  263. DATA 0,0,0,142,0,0,0,0,0,0,0,2,0,0,0
  264. DATA 0,2,0,0,0,142,0,0,0,0,0,0,0,0,0
  265. DATA 0,0,0,0,0,0,0,0,0,2,0,0,147,0,0
  266. DATA 0,142,0,0,0,0,0,0,0,0,0,0,0,0,0
  267. DATA 0,0,0,2,0,0,142,0,0,2,0,0,0,2,0
  268. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  269. DATA 2,0,0,0,0,145,0,2,0,0,0,0,2,0,0
  270. DATA 0,0,0,2,0,0,0,0,0,0,0,0,0,0,142
  271. DATA 145,0,0,0,147,0,0,0,2,0,0,2,0,0,0
  272.  
  273. treedata:
  274. DATA 0,0,0,2,2,2,2,2,2,2,2,0,0,0,0
  275. DATA 0,0,2,2,2,2,2,2,2,2,2,2,0,0,0
  276. DATA 0,6,2,2,2,2,2,2,2,2,2,6,0,0,0
  277. DATA 0,2,6,2,2,2,2,2,2,2,6,2,0,0,0
  278. DATA 0,2,2,6,2,2,2,2,2,6,2,2,0,0,0
  279. DATA 0,0,2,2,6,2,2,2,6,2,2,0,0,0,0
  280. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  281. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  282. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  283. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  284. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  285. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  286. DATA 0,0,0,0,0,6,6,6,0,0,0,0,0,0,0
  287. DATA 0,0,0,0,6,6,6,6,6,0,0,0,0,0,0
  288. DATA 0,0,0,6,6,6,6,6,6,6,0,0,0,0,0
  289.  
  290. playerdata:
  291. DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  292. DATA 0,0,0,0,0,0,7,7,7,0,0,0,1,0,0
  293. DATA 0,0,0,0,0,0,7,7,7,0,0,0,1,0,0
  294. DATA 0,0,0,0,0,0,7,7,7,0,0,0,1,0,0
  295. DATA 0,0,0,0,0,0,0,7,0,0,0,0,1,0,0
  296. DATA 0,0,0,0,0,0,0,7,0,0,0,0,1,0,0
  297. DATA 0,2,0,2,0,0,0,7,0,0,0,1,1,1,0
  298. DATA 0,2,2,7,7,7,7,7,7,7,7,7,7,0,0
  299. DATA 0,2,2,2,0,0,0,7,0,0,0,0,1,0,0
  300. DATA 0,0,2,0,0,0,0,7,0,0,0,0,1,0,0
  301. DATA 0,0,0,0,0,0,0,7,0,0,0,0,0,0,0
  302. DATA 0,0,0,0,0,0,7,0,7,0,0,0,0,0,0
  303. DATA 0,0,0,0,0,7,0,0,0,7,0,0,0,0,0
  304. DATA 0,0,0,0,7,0,0,0,0,0,7,0,0,0,0
  305. DATA 0,0,0,7,0,0,0,0,0,0,0,7,0,0,0
  306.  
  307.